//
//  HPAccountInfoViewController.m
//  HPZZTong
//
//  Created by Handpay on 14-3-27.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPAccountInfoViewController.h"
#import "ZZTongUtility.h"

@interface HPAccountInfoViewController ()
@property(nonatomic, retain)NSMutableArray* baseInfoDataArray;
@property(nonatomic, retain)NSArray* modifyDataArray;
@end

@implementation HPAccountInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.baseInfoDataArray = [NSMutableArray arrayWithArray:@[@{kDes: @"账号:", kValue:ZZT_CLIENG.accountInfo.account},@{kDes: @"手机号码:", kValue:[ZZTongUtility maskMobile:ZZT_CLIENG.accountInfo.mobile]}]];
        self.modifyDataArray = @[@"修改手机号码", @"修改登录密码"];
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"基本信息管理";
    [super viewDidLoad];
    
    self.baseInfoTableView.dataSource = self;
    self.modifyTableView.dataSource = self;
    self.modifyTableView.delegate = self;
    
    if (ZZT_CLIENG.accountInfo.accountStatus == AccountStatusReview) {
        CGRect frame = self.modifyTableView.frame;
        frame.size.height = 44 - 1;
        self.modifyTableView.frame = frame;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSString* mobile = [[self.baseInfoDataArray objectAtIndex:1] objectForKey:kValue];
    if ([mobile isEqualToString:ZZT_CLIENG.accountInfo.mobile]) {
        return;
    }
    [self.baseInfoDataArray replaceObjectAtIndex:1 withObject:@{kDes: @"手机号码:", kValue:[ZZTongUtility maskMobile:ZZT_CLIENG.accountInfo.mobile]}];
    [self.baseInfoTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (ZZT_CLIENG.accountInfo.accountStatus == AccountStatusReview && ([tableView isEqual:self.modifyTableView])) {
        return 1;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView isEqual:self.baseInfoTableView] ? [self initBaseInfoTableViewCell:indexPath.row] : [self initModifyTableViewCell:indexPath.row];
}

- (UITableViewCell *)initBaseInfoTableViewCell:(NSInteger)index{
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    NSDictionary* infoDic = [self.baseInfoDataArray objectAtIndex:index];
    
    UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 90, 44)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [infoDic objectForKey:kDes];
    [cell addSubview:desLabel];
    [desLabel release];
    
    UILabel* valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 44)];
    valueLabel.textColor = kNormalFontColor;
    valueLabel.font = kNormalFont;
    valueLabel.text = [infoDic objectForKey:kValue];
    [cell addSubview:valueLabel];
    [valueLabel release];
    
    return [cell autorelease];
}

- (UITableViewCell *)initModifyTableViewCell:(NSInteger)index{
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 290, 44)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = [UIFont systemFontOfSize:18];
    if (ZZT_CLIENG.accountInfo.accountStatus == AccountStatusReview) {
        desLabel.text = [self.modifyDataArray objectAtIndex:index + 1];
    }else{
        desLabel.text = [self.modifyDataArray objectAtIndex:index];
    }
    [cell addSubview:desLabel];
    [desLabel release];
    
    return [cell autorelease];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (ZZT_CLIENG.accountInfo.accountStatus == AccountStatusReview) {
        if (indexPath.row == 0) {
            [HPUIENG showScreen:SCR_MODIFY_PWD];
            return;
        }
    }
    if (indexPath.row == 0) {
        [HPUIENG showScreen:SCR_VERIFY_CURRENT_MOBILE];
    }else if (indexPath.row == 1){
        [HPUIENG showScreen:SCR_MODIFY_PWD];
    }
}

- (void)dealloc {
    self.baseInfoDataArray = nil;
    self.modifyDataArray = nil;
    
    [_baseInfoTableView release];
    [_modifyTableView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setBaseInfoTableView:nil];
    [self setModifyTableView:nil];
    [super viewDidUnload];
}
@end
